package ray;

import java.io.File;
import java.util.ArrayList;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

import ray.Ray;
import ray.math.*;
import ray.surface.Surface;


/**
 * A simple ray tracer.
 *
 * @author ags
 */
public class RayTracer {

	/**
	 * The main method takes all the parameters an assumes they are input files
	 * for the ray tracer. It tries to render each one and write it out to a PNG
	 * file named <input_file>.png.
	 *
	 * @param args
	 */
	public static final void main(String[] args) {

		//file chooser if we so desire (change = args[ctr] to = fileName)
		
		/*JFileChooser chooser = new JFileChooser();
		String fileName = new String();
		chooser.setCurrentDirectory(new File("."));
		int returnVal = chooser.showOpenDialog(chooser);
	    if(returnVal == JFileChooser.APPROVE_OPTION) {
	    	fileName = chooser.getSelectedFile().getAbsolutePath();
	    }
	    else System.exit(0);*/
	    
	    
		Parser parser = new Parser();
		for (int ctr = 0; ctr < args.length; ctr++) {

			// Get the input/output filenames.
			String inputFilename = args[ctr];
			String outputFilename = inputFilename + ".png";

			// Parse the input file
			Scene scene = (Scene) parser.parse(inputFilename, Scene.class);

			// Render the scene
			renderImage(scene);

			// Write the image out
			scene.getImage().write(outputFilename);
		}
	}

	/**
	 * The renderImage method renders the entire scene.
	 *
	 * @param scene The scene to be rendered
	 */
	public static void renderImage(Scene scene) {

		// Get the output image
		Image image = scene.getImage();
		Camera camera = scene.getCamera();
		camera.setup();

		// Timing counters
		long startTime = System.currentTimeMillis();
		
		int height = image.height;
		int width = image.width;
		
		int i,j;
		for(i=0; (i<height); i++)
		{
			for(j=0; (j<width); j++)
			{
				double u = (j+0.5)/width;
				double v = (i + 0.5)/height;
				
				//Compute the ray from camera through pixel
				Ray ray = new Ray();
				Color color = new Color();
				camera.makeRay(ray, u, v);
				rayTrace(color, ray, scene, scene.getLights(), scene.getSurfaces());
				color.gammaCorrect(1);
				color.clamp(0, 1);
				image.setPixelColor(color, j, i);
			}
		}

		/*
		 * Render the image, writing the pixel values into image.
		 */

		// Output time
		long totalTime = (System.currentTimeMillis() - startTime);
		System.out.println("Done.  Total rendering time: "
				+ (totalTime / 1000.0) + " seconds");
	}

	public static void rayTrace(Color color, Ray ray, Scene scene, ArrayList<Light> lights, ArrayList<Surface> surfaces)	{
		int i = 0;
		boolean intersection;
		Intersection inter = new Intersection();
		do{
			intersection = surfaces.get(i).intersect(inter, ray);
			if(intersection){
				Vector3 toEye = new Vector3();
				//ray from intersection to camera
				toEye.sub(scene.camera.viewPoint, inter.intersection);
				inter.surface.getShader().shade(color, scene, lights, toEye, inter);
				return;
			}
			else{ 
				color.set(0,0,0);
			}
			i++;
			
		}while(i < surfaces.size());
	}
}
